/*
 * DevicesLed.h
 *
 *      Author: Honrun
 */

#ifndef BSP_DEVICESLED_H_
#define BSP_DEVICESLED_H_


/* LED状态机执行周期（ms） */
#define LED_EXECUTION_PERIOD        20
/* 快闪周期（ms） */
#define LED_FLASH_FAST_PERIOD       1000
/* 慢闪周期（ms） */
#define LED_FLASH_SLOW_PERIOD       2000
/* SOS模式，周期间隔时间（ms） */
#define LED_SOS_DELAY_PERIOD        2000


/* 驱动模式 */
#define LED_DRIVE_NULL              0
#define LED_DRIVE_IO                1
#define LED_DRIVE_PWM               2
/* 亮度等级 */
#define LED_HIGH_DUTY               1.0f
#define LED_LOW_DUTY                0.5f


/* 指示灯 */
#define LED_1_GPIO_Port             GPIOB
#define LED_1_Pin                   GPIO_PIN_1


typedef enum {
    LED_CHANNEL_1                   = 0x01,
    LED_CHANNEL_2                   = 0x02,
    LED_CHANNEL_3                   = 0x04,
    LED_CHANNEL_4                   = 0x08,
    LED_CHANNEL_ALL                 = 0x7F,
} LedChannelEnum;


typedef enum {
    LED_DISABLE                     = 0,    /* 关闭 */
    LED_ENABLE                      = 1,    /* 常亮 - 高亮*/
    LED_ENABLE_LOW                  = 3,    /* 常亮 - 低亮 */
    LED_DUTY                        = 4,    /* 固定占空比 */

    LED_FLASH_FAST                  = 10,   /* 快速频率（1Hz） */
    LED_FLASH_FAST_ENABLE_CNT       = 11,   /* 闪烁N次后 常亮 */
    LED_FLASH_FAST_DISABLE_CNT      = 12,   /* 闪烁N次后 关闭 */

    LED_FLASH_SLOW                  = 20,   /* 低速频率（2Hz） */
    LED_FLASH_SLOW_ENABLE_CNT       = 21,   /* 闪烁N次后 常亮 */
    LED_FLASH_SLOW_DISABLE_CNT      = 22,   /* 闪烁N次后 关闭 */

    LED_BREATHE                     = 30,   /* 呼吸 */

    LED_IDLE                        = 0xFF,
} LedStateEnum;


typedef struct{
    LedStateEnum state;                     /* 状态 */
    int16_t cnt;                            /* 闪烁次数 */
    int8_t duty;                            /* 占空比 */

    uint8_t driveMode;                      /* 驱动模式，0：普通IO；1：PWM */
    uint32_t periph;
    uint32_t channel;
}LedType;


typedef struct{
    LedType user1;                          /* 用户 指示灯1 */
}LedInfoType;


void vLedInit(void);
void vLedOpen(uint32_t uiChannel);
void vLedClose(uint32_t uiChannel);
void vLedRevesal(uint32_t uiChannel);

void vLedMachine(void);
void vLedStatusSet(LedChannelEnum usChannel, LedStateEnum enumStatus, uint8_t ucCntOrDuty);
#define vLedFlashFast(usChannel)    vLedStatusSet((usChannel), LED_FLASH_FAST,  0)
#define vLedFlashSlow(usChannel)    vLedStatusSet((usChannel), LED_FLASH_SLOW,  0)
#define vLedBreathe(usChannel)      vLedStatusSet((usChannel), LED_BREATHE,     0)
#define vLedEnable(usChannel)       vLedStatusSet((usChannel), LED_ENABLE,      0)
#define vLedDisable(usChannel)      vLedStatusSet((usChannel), LED_DISABLE,     0)
LedInfoType *ptypeLedGetInfo(void);


#endif /* BSP_DEVICESLED_H_ */
